//
// Created by user on 20-5-5.
//

#include "Atomic.h"

inline void atomicAdd(Atomic_T *atomic, long value) {
    __asm__ __volatile__    (    "lock	addq	%1,	%0	\n\t"
    :"=m"(atomic->value)
    :"r"(value)
    :"memory"
    );
}

inline void atomicSub(Atomic_T *atomic, long value) {
    __asm__ __volatile__    (    "lock	subq	%1,	%0	\n\t"
    :"=m"(atomic->value)
    :"r"(value)
    :"memory"
    );
}

inline void atomicInc(Atomic_T *atomic) {
    __asm__ __volatile__    (    "lock	incq	%0	\n\t"
    :"=m"(atomic->value)
    :"m"(atomic->value)
    :"memory"
    );
}

inline void atomicDec(Atomic_T *atomic) {
    __asm__ __volatile__    (    "lock	decq	%0	\n\t"
    :"=m"(atomic->value)
    :"m"(atomic->value)
    :"memory"
    );
}

inline void atomicSetMask(Atomic_T *atomic, long mask) {
    __asm__ __volatile__    (    "lock	orq	%1,	%0	\n\t"
    :"=m"(atomic->value)
    :"r"(mask)
    :"memory"
    );
}

inline void atomicClearMask(Atomic_T *atomic, long mask) {
    __asm__ __volatile__    (    "lock	andq	%1,	%0	\n\t"
    :"=m"(atomic->value)
    :"r"(~(mask))
    :"memory"
    );
}